<template>
<!--withdrawals.wxml-->
<view class="withdraw_container">
	<view class="withdraw-header-wrap">
		<view class="can_money_tit"><text>￥</text><text>可提现总金额</text></view>
		<view class="can_money_con"><text>￥</text><text>{{userMoney}}</text></view>			
	</view>
    <view class="withdraw_style">
        <view class="withdraw_stna">提现方式</view>
        <view class="withdraw_stcon">
            <image src="/static/images/withdraw-weix.png"></image>
            <text class="wh_user_account" @tap="bindWechat">{{cashConfig.openid?'已绑定微信':'还未绑定微信'}}</text>
        </view>
    </view>
    <view class="withdraw-cont-wrap">
		<view class="withdraw-cont-p1">提现金额</view>
		<view class="withdraw-money">
			<text>￥</text>
			<input type="digit" name="money" id="money" :value="money" @input="withdrawMoney"></input>
		</view>
		<view class="withdraw-allmoney">
			<text>手续费:{{withdrawFee}}</text>
            <button class="wh_allmony" @tap="allWithdraw">全部提现</button>
		</view>
	</view>
    <view class="withdraw-cont-wrap">
        <view class="withdraw-cont-p2">
            <text>支付密码：</text>
            <input name="paypwd" id="paypwd" type="password" placeholder="请填写支付密码" @blur="setPwd"></input>
        </view>
		<view class="wh_setpsd">
            <navigator @tap.stop="userinfoEdit" data-url="/pages/user/userinfo_edit/userinfo_edit?type=paypwd">前往设置或修改交易密码</navigator>
		</view>
		<view class="withdraw-sub-btn">
            <button form-type="submit" @tap="submit">提交申请</button>
        </view>
	</view>
    <view class="withdraw-foot-wrap">
        <text>温馨提示：</text>
        <text>1.提现金额须大于{{cashConfig.min_cash}}元，小于{{cashConfig.max_cash}}元</text>
        <text>2.提现收取{{cashConfig.service_ratio}}%的手续费，每笔最低元手续费</text>
        <text>3.手续费在到账金额中扣除</text>
        <text>4.提现审核一般3 - 5个工作日到账</text>
	</view>
</view>
</template>

<script>
var app = getApp();
var request = app.globalData.request;
var md5 = require("../../../utils/md5.js");

export default {
  data() {
    return {
      url: app.globalData.setting.url,
      userMoney: 0,
      cashConfig: null,
      money: '',
      withdrawFee: 0.00,
      pwd: ''
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    this.setData({
      userMoney: options.money
    });
    this.getCashConfig();
  },

  onShow() {
    uni.removeStorageSync('withdrawals_pay');
  },

  methods: {
    getCashConfig: function () {
      var that = this;
      request.post('/api/user/cash_config', {
        success: function (res) {
          that.setData({
            cashConfig: res.data.result
          });
        }
      });
    },
    userinfoEdit: function (e) {
      console.log(e);

      if (!app.globalData.userInfo.mobile) {
        app.globalData.showTextWarining('请先设置手机号码', function () {
          uni.setStorageSync('withdrawals_pay', true);
          uni.navigateTo({
            url: '/pages/user/userinfo_edit/userinfo_edit?type=mobile'
          });
        });
      } else {
        uni.navigateTo({
          url: e.currentTarget.dataset.url
        });
      }
    },
    bindWechat: function () {
      var that = this;

      if (that.cashConfig.openid == '') {
        return app.globalData.showTextWarining("请打开电脑端扫码绑定微信");
      }
    },
    withdrawMoney: function (e) {
      var money = e.detail.value;
      this.setData({
        money: money
      });
      var m = 0,
          s1 = money.toString(),
          s2 = (this.cashConfig.service_ratio * 0.01).toString();

      try {
        m += s1.split(".")[1].length;
      } catch (e) {}

      try {
        m += s2.split(".")[1].length;
      } catch (e) {}

      var fee = Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);

      if (!this.cashConfig.service_ratio) {
        this.setData({
          withdrawFee: 0.00
        });
      } else {
        if (fee < this.cashConfig.min_service_money) {
          this.setData({
            withdrawFee: this.cashConfig.min_service_money
          });
        } else if (fee > this.cashConfig.max_service_money) {
          if (this.cashConfig.max_service_money > 0) {
            this.setData({
              withdrawFee: this.cashConfig.max_service_money
            });
          } else {
            this.setData({
              withdrawFee: fee
            });
          }
        } else {
          this.setData({
            withdrawFee: fee
          });
        }
      }
    },
    setPwd: function (e) {
      this.setData({
        pwd: e.detail.value
      });
    },
    allWithdraw: function () {
      this.setData({
        money: this.userMoney
      });
      var m = 0,
          s1 = this.userMoney.toString(),
          s2 = (this.cashConfig.service_ratio * 0.01).toString();

      try {
        m += s1.split(".")[1].length;
      } catch (e) {}

      try {
        m += s2.split(".")[1].length;
      } catch (e) {}

      var fee = Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);

      if (!this.cashConfig.service_ratio) {
        this.setData({
          withdrawFee: 0.00
        });
      } else {
        if (fee < this.cashConfig.min_service_money) {
          this.setData({
            withdrawFee: this.cashConfig.min_service_money
          });
        } else if (fee > this.cashConfig.max_service_money) {
          if (this.cashConfig.max_service_money > 0) {
            this.setData({
              withdrawFee: this.cashConfig.max_service_money
            });
          } else {
            this.setData({
              withdrawFee: fee
            });
          }
        } else {
          this.setData({
            withdrawFee: fee
          });
        }
      }
    },
    submit: function () {
      var that = this;

      if (that.cashConfig.openid == '') {
        return app.globalData.showTextWarining("请打开电脑端扫码绑定微信");
      }

      if (that.money == '') {
        return app.globalData.showTextWarining("请输入金额");
      }

      if (that.money <= 0) {
        return app.globalData.showTextWarining("请输入正确的金额");
      }

      if (that.pwd == '') {
        return app.globalData.showTextWarining("请输入支付密码");
      }

      request.post('/api/user/withdrawals', {
        data: {
          bank_name: '微信',
          bank_card: '',
          money: that.money,
          paypwd: md5('TPSHOP' + that.pwd),
          usermoney: that.userMoney
        },
        success: function (res) {
          app.globalData.showSuccess('申请成功', function () {
            uni.navigateBack({
              delta: 1
            });
          });
        }
      });
    }
  }
};
</script>
<style>
/* withdrawals.wxss */

page{
  background-color: #fff;
}
.withdraw_container{
  width: 100%;
}
.withdraw-header-wrap{
	height: 180rpx;
  background-color: #ff6a00;
  color: #fff;
  padding: 60rpx 30rpx;
}
.can_money_tit{
  font-size: 30rpx;
  line-height: 36rpx;
}
.can_money_tit text:first-child{
  display: inline-block;
  width: 36rpx;
  height: 36rpx;
  text-align: center;
  font-size: 24rpx;
  border: 1px solid #fff;
  border-radius: 50%;
  margin-right: 20rpx;
}
.can_money_con{
  margin-top: 66rpx;
  font-size: 24rpx;
}
.can_money_con text{
  font-size: 54rpx;
  margin-left: 20rpx;
}
.withdraw_style{
  padding: 30rpx 30rpx;
}
.withdraw_stna{
  font-size: 30rpx;
}
.withdraw_stcon{
  margin-top: 20rpx;
  line-height: 51rpx;
  overflow: hidden;
}
.withdraw_stcon image{
  width: 51rpx;
  height: 51rpx;
  float: left;
}
.withdraw_stcon .wh_user_account{
  font-size: 28rpx;
  float: left;
  margin-left: 20rpx;
}
.withdraw-cont-wrap{
	padding: 0 30rpx;
  border-top: 18rpx solid #f3f3f3;
  font-size: 30rpx;
}
.withdraw-cont-wrap .withdraw-cont-p1{
	padding: 40rpx 0 20rpx;
	height: 30rpx;
	color: #4c4c4c;
	font-size: 26rpx;
}
.withdraw-cont-wrap .withdraw-cont-p1 text{
  margin-left: 10rpx;
}
.withdraw-money{
  overflow: hidden;
  padding-top: 20rpx;
}
.withdraw-money text{
  float: left;
}
.withdraw-money input{
  width: 94%;
  border-bottom: 1px solid #f3f3f3;
  float: right;
  font-size: 36rpx;
}
.withdraw-allmoney{
  height: 90rpx;
  line-height: 90rpx;
  font-size: 30rpx;
}
.withdraw-allmoney text{
  float: left;
}
.withdraw-allmoney .wh_allmony{
  float: right;
  width: 140rpx;
  height: 90rpx;
  line-height: 90rpx;
  color: #ff6a00;
  background-color: transparent;
}
button::after {
  border: none;
}
.withdraw-cont-wrap .withdraw-cont-p2{
	padding-top:20rpx;
  height: 90rpx;
  line-height: 90rpx; 
}
.withdraw-cont-p2{
  border-bottom: 1px solid #f3f3f3;
}
.withdraw-cont-p2 text{
  float: left;
}
.withdraw-cont-p2 input{
  float: left;
  height: 90rpx;
  line-height: 90rpx;
  margin-left: 10rpx;
}
.wh_setpsd{
  width: 100%;
  overflow: hidden;
}
.wh_setpsd navigator{
  float: right;
  height: 72rpx;
  line-height: 72rpx;
  color:#ff6a00;
  background-color: transparent; 
}
.withdraw-sub-btn{
  margin: 75rpx 0;
}
.withdraw-sub-btn button{
	width: 100%;
	height: 88rpx;
  line-height: 88rpx;
	color: #ffffff;
	background-color: #e73223;
	font-size: 36rpx;
	border-radius:10rpx;
}
.withdraw-foot-wrap{
  padding: 0 30rpx;
  font-size: 24rpx;
  color: #666;
  margin-bottom: 60rpx;
}
.withdraw-foot-wrap text{
  display: block;
  line-height: 36rpx;
}



</style>